define([ "app", "jquery", "md5", "ecb","artDialog","iframeTools", "services/UserService",'directives/ImageCrop',
		"resources/UserResource","resources/ContactsResource", "resources/ShowTimesUserResource" ],
		function(app) {

			var deps = [ "$scope", "$state", "$window", "$location", "$stateParams", "UserService", "UserResource","ContactsResource", "ShowTimesUserResource" ];

			function controller($scope, $state, $window, $location, $stateParams, UserService, UserResource,ContactsResource, ShowTimesUserResource) {
				
				$scope.userId = $stateParams.userId;
				// 初始化数据
				$scope.userTypeList = [
				    {id : "0", name : "普通用户"},
				    {id : "2", name : "后台用户"}
				];
				
				$scope.groupList = [
				    {id : "1", name : "产品岗"},
				    {id : "2", name : "测试岗"},
				    {id : "3", name : "开发岗"},
				    {id : "4", name : "管理岗"}
				];
				
				$scope.positionList = [ {
					id : "1",
					name : "产品工程师"
				}, {
					id : "2",
					name : "产品经理"
				} ];
				
				$scope.sexList = [
				    {id : "1", name : "男"},
				    {id : "2", name : "女"}
				];
				
				initData();
				
				function initData(){
					ShowTimesUserResource.getUserById($scope.userId).success(function(data){
						if (data.resCode=='0000') {
							$scope.userVo =data.obj;
							$scope.sex=""+data.obj.sex;
							$scope.userType=""+data.obj.userType;
							$scope.groupId=""+data.obj.groupId;
							// 根据groupid 设置positionid
							$scope.checkGroup(data.obj.positionId);
							
							$scope.positionId=""+data.obj.positionId;
						}else{
							art.dialog.alert("获取用户失败");
						}
		            });
				}
				var imageData;
				
				$scope.updateUser = function() {

					if (imageData && imageData.file) {
	                    uploadImage().success(function(result) {
	                        $scope.userVo.userPicUrl = eval("(" + result.fileId + ")").obj;
	                        saveOrUpdate();
	                    });
	                } else {
	                	saveOrUpdate();
	                }
				};

				
				 /**
	             * 图片数据
	             */
	            $scope.$bus.subscribe({
	                channel: 'image',
	                topic: 'crop',
	                callback: function(data) {
	                    imageData = data;
	                }
	            });
	            
				$scope.close = function() {
					$state.go('home.userManagement');
				}
				
				/**
				 * 选择岗位
				 */
				$scope.checkposition =function (){
					
					var positionId = $scope.positionId;
					if (positionId == null || positionId == "" || typeof (positionId) == 'undefined') {
						$scope.userCreateForm.positionId.$error.required=true;
						$scope.userCreateForm.positionId.$dirty=true;
					}else{
						$scope.userCreateForm.positionId.$error.required=false;
						$scope.userCreateForm.positionId.$dirty=false;
					}
				}
				

				/**
				 * 选择岗位类型
				 */
	            $scope.checkGroup = function(positionId) {
					var groupId = $scope.groupId;
					if (groupId == null || groupId == "" || typeof (groupId) == 'undefined') {
						$scope.positionList = [
								
						];
						$scope.userCreateForm.groupId.$error.required=true;
						$scope.userCreateForm.groupId.$dirty=true;
					}else{
						$scope.userCreateForm.groupId.$error.required=false;
						$scope.userCreateForm.groupId.$dirty=false;
					}
					if (groupId == "1") {
						$scope.positionList = [ {
							id : "1",
							name : "产品工程师"
						}, {
							id : "2",
							name : "产品经理"
						} ];
						
					}

					if (groupId == "2") {
						$scope.positionList = [ {
							id : "1",
							name : "测试工程师"
						} ];
					}

					if (groupId == "3") {
						$scope.positionList = [ {
							id : "1",
							name : "实习生"
						}, {
							id : "2",
							name : "java初级开发"
						}, {
							id : "3",
							name : "java中级开发"
						}, {
							id : "4",
							name : "java高级开发"
						}, {
							id : "5",
							name : "java架构师"
						} ];
						$scope.positionId=positionId;
					}

					if (groupId == "4") {
						$scope.positionList = [ {
							id : "1",
							name : "部门经理"
						}, {
							id : "2",
							name : "湖北研发中心主任"
						} ];
					}
					
				}
				
				function saveOrUpdate(){
					if ($scope.userCreateForm.$invalid) {
						return false;
					}
					$scope.userCreateForm.$invalid = true;
					
					// 修改下拉框的值
					$scope.userVo.sex=$scope.sex;
					$scope.userVo.userType=$scope.userType;
					$scope.userVo.groupId=$scope.groupId;
					$scope.userVo.positionId=$scope.positionId;
					
					ShowTimesUserResource.update($scope.userVo).success(function(data){
						$scope.userCreateForm.$invalid = false;
						if (data.resCode=='0000') {
							//$modalInstance.close($scope.userVo);
						}else{
							art.dialog.alert("修改用户失败");
							//$modalInstance.close($scope.userVo);
						}
						$state.go('home.userManagement');
		            });
					
				}
				
				
				/** 按钮状态 **/
				$scope.$watch('userCreateForm.$invalid', function(value) {
					// 无效
					if(value){
						$("#confirm").addClass("confirm_btn_x");
						$("#confirm").removeClass("confirm_btn1 ");
					}
					else{
						$("#confirm").addClass("confirm_btn1");
						$("#confirm").removeClass("confirm_btn_x ");
					}
		        });
				
				/**
	             * 图片剪裁
	             */
	            function uploadImage() {
	                var formData = new FormData();
	                formData.append("file", imageData.file);

	                var cropParam = {
	                    cropX: imageData.cropX || 0,
	                    cropY: imageData.cropY || 0,
	                    cropW: imageData.cropW || 0,
	                    cropH: imageData.cropH || 0
	                };
	                return ContactsResource.uploadCropImage(formData, cropParam);
	            }
	            
			}
			
			
			
			/**
			 * 密码 encrypt 加密
			 */
			function encrypt(content,srcKey){
			      var keyLen = 8;
				  var srcKeyLen = srcKey.length;
				
				 if(srcKeyLen > keyLen){
					 srcKey = srcKey.substr(0,keyLen);
					
				 }else{
					 var paddLen = keyLen -  srcKeyLen;
					 for(var i=0;i<paddLen;i++){
						 srcKey += "0";
					 }
				 }
				
				 var key = CryptoJS.enc.Utf8.parse(srcKey);		
				 var encryptStr =  CryptoJS.DES.encrypt(content, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
				return encryptStr.toString();
			}
			
			controller.$inject = deps;
			return app.lazy.controller("EditUserController", controller);
		});
